package mudus;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Gateway {

    private ServerSocket listenerSocket;    

    public Gateway() {
        try {
            listenerSocket = new ServerSocket(GlobalSettings.GATEWAY_PORT);
            System.out.println("Menunggu koneksi");
        } catch (IOException ex) {
            Logger.getLogger(Gateway.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void start() {
        while(true) {
            try {
                Socket incomeConnection = listenerSocket.accept();                
                System.out.println("Koneksi diterima dari " + incomeConnection.getInetAddress().getHostName() + " port : " + incomeConnection.getPort());
                ConnectionHandler ch = new ConnectionHandler(this, incomeConnection);
                new Thread(ch).start();
            } catch (IOException ex) {
                Logger.getLogger(Gateway.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void main(String[] args) {
        System.out.println("memulai server");
        Gateway gateway = new Gateway();
        gateway.start();
    }

}
